<template>
  <div class="air">
    <el-row :gutter="20">
        <el-col :span="24" class="paddCol" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                  <span :style="this.alarmstate == 0 ? '':'color:#ea2e2e'"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}}</span></span>
              </div>
              <div class="temp_card">
                <div class="xtg402_data">
                  <div class="irf_allk">
                    <div class="irf_nr_top irf_nr_top_margin">
                      <!-- xg_air_active_open 灰色图标-->
                       <!-- 0关机  1开机  2制热  3制冷 -->
                        <!-- 1： 26  2： 24  3： 22  4：20 -->
                      <!-- 开机 -->
                      <div class="irf_top_l">
                        <div :class="this.dataShow[8].value == 0 ? 'control_xg_img3 xg_air_active_open': 'control_xg_img3'"></div>
                          <div class="irf_but" @click="switchOpen(8,1)">{{this.data.unit[3].name}}</div>
                      </div>
                      <!-- 关机 -->
                      <div class="irf_top_r">
                        <div :class="this.dataShow[8].value == 0 ? 'control_xg_img4': 'control_xg_img4 xg_air_active_open'"></div>
                          <div class="irf_but" @click="switchOpen(8,0)">{{this.data.unit[2].name}}</div>
                      </div>
                    </div>
                    <div class="irf_nr_top">
                      <!-- 制热 -->
                      <div class="irf_top_l">
                        <div :class="this.dataShow[8].value == 2 ? 'control_xg_img2': 'control_xg_img2 xg_air_active_open'"></div>
                          <div class="irf_but" @click="switchOpen(8,2)">{{this.data.unit[0].name}}</div>
                      </div>
                      <!-- 制冷 -->
                      <div class="irf_top_r">
                        <div :class="this.dataShow[8].value == 3 ? 'control_xg_img1': 'control_xg_img1 xg_air_active_open'"></div>
                          <div class="irf_but" @click="switchOpen(8,3)">{{this.data.unit[1].name}}</div>
                      </div>
                    </div>
                    <div class="irf_nr_top">
                      <div class="irf_nr_flex" v-if="this.data.unit[4].ready == 1" >
                        <div class="irf_nr_flex_k" @click="switchOpen(9,1)" >
                          <svg :class="this.dataShow[9].value == 1 && this.dataShow[8].value != 0 ? 'icon anjian_active':'icon anjian'" aria-hidden="true">
                            <use xlink:href="#icon-00-wendushezhi-05"></use>
                          </svg>
                          <div class="irf_but">{{this.data.unit[4].name}}</div>
                        </div>
                      </div>
                      <div class="irf_nr_flex" v-if="this.data.unit[5].ready == 1" >
                        <div class="irf_nr_flex_k" @click="switchOpen(9,2)" >
                          <svg :class="this.dataShow[9].value == 2 && this.dataShow[8].value != 0 ? 'icon anjian_active':'icon anjian'" aria-hidden="true">
                            <use xlink:href="#icon-00-wendushezhi-05"></use>
                          </svg>
                          <div class="irf_but">{{this.data.unit[5].name}}</div>
                        </div>
                      </div>
                      <div class="irf_nr_flex" v-if="this.data.unit[6].ready == 1" >
                        <div class="irf_nr_flex_k" @click="switchOpen(9,3)" >
                          <svg :class="this.dataShow[9].value == 3 && this.dataShow[8].value != 0  ? 'icon anjian_active':'icon anjian'" aria-hidden="true">
                            <use xlink:href="#icon-00-wendushezhi-05"></use>
                          </svg>
                          <div class="irf_but">{{this.data.unit[6].name}}</div>
                        </div>
                      </div>
                      <div class="irf_nr_flex" v-if="this.data.unit[7].ready == 1" >
                        <div class="irf_nr_flex_k" @click="switchOpen(9,4)" >
                          <svg :class="this.dataShow[9].value == 4 && this.dataShow[8].value != 0  ? 'icon anjian_active':'icon anjian'" aria-hidden="true">
                            <use xlink:href="#icon-00-wendushezhi-05"></use>
                          </svg>
                          <div class="irf_but">{{this.data.unit[7].name}}</div>
                        </div>
                      </div>
                    </div>
                    <div class="irf_nr">
                      <div class="irf_nr_bot_flex" v-for="item in this.dataShow" v-if="item.id != 8 && item.id != 9 && item.ready == 1" :key="item.index" >
                        <div class="irf_nr_bot_flex_k" @click="switchOpen(item.id)" >
                          <svg :class="item.value == 1 ? 'icon anjian_bot_active':'icon anjian_bot'" aria-hidden="true">
                            <use xlink:href="#icon-xuanfuanniufadongtai"></use>
                          </svg>
                          <div class="irf_bot_but">{{item.name}}</div>
                          <div class="irf_bot_botbk"></div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="temp_time_mode">
                    <div class="temp_peak_name">
                      <i class="el-icon-time"></i>
                      时间统计
                    </div>
                    <div class="temp_time">
                      <div class="temp_time_line">
                        <p class="temp_warning_title">首次上线：</p>
                        <p class="temp_time_year">{{ this.firstconnecttimeYear }} </p> 
                        <p class="temp_time_hour">{{ this.firstconnecttimeHour}}</p>
                      </div>
                      <div class="temp_time_line" v-if="this.connecttime > 0">
                        <p class="temp_warning_title">最近上线：</p>
                        <p class="temp_time_year">{{ this.connecttimeYear }} </p>
                        <p class="temp_time_hour">{{ this.connecttimeHour }}</p>
                      </div>
                      <div class="temp_time_line" v-if="this.devtimeshow == 1">
                        <p class="temp_warning_title">累计时长：</p>
                        <p class="temp_time_h"> {{this.totaltime}} </p> <span class="temp_time_s"> h</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
        </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: ["msg"],
  data() {
    return {
      alarmstate:0,//在线离线
      timeGetData:'',//接口方法的定时器
      data: "", //当前设备数据  真实数据
      dataShow: "", //当前设备数据  展示用数据
      clickGetData:'',//延迟保存计时器
      clickNumber:0,//延迟保存点击次
      dataor:1,//判断接口是否成功，1表示有返回成功，0表示失败，3之内无返回就置0表示失败
      suspend:0,
      //属性数据
      firstconnecttimeYear:'',//首次上线年月日
      firstconnecttimeHour:'',//首次上线时分
      connecttime:0,//最近上线时间戳
      connecttimeYear:'',//最近上线年月日
      connecttimeHour:'',//最近上线时分
      totaltime:0,//累计运行时间
      devtimeshow:localStorage.getItem('累计时长'),//累计运行时间显示隐藏
      localname:'',//本地储存名称
    };
  },
  mounted() {
  },
  created() {
    // 判断是否是直接访问当前设备页面
    if(localStorage.getItem('当前访问路径') == 'overview2'){
      this.localname = "6.0展示设备卡片"
    }else{
      this.localname = "当前设备编号"
    }
    this.info = JSON.parse(localStorage.getItem(this.localname))
    var sss = JSON.parse(localStorage.getItem("menuDataJson")).unit
    var dd = []
    for(var a = 0;  a < 10; a++){
      dd.push(
        {
          id:sss[a].id, 
          value:sss[a].value,
          name:sss[a].name,
          ready:sss[a].ready
        }
      )
    }
    this.dataShow = dd
    this.data = JSON.parse(localStorage.getItem("menuDataJson"))
    this.getData(JSON.parse(localStorage.getItem(this.localname)))
    this.getDataStart()
  },
  methods: {
    //重新定时走接口
    getDataStart(){
      this.$nextTick(() => {
          this.timeGetData = setInterval(() => {
            if(this.dataor == 0){ //接口失败提示
              this.$message({
                message: '服务器访问失败',
                type: 'error',
                duration: 1000
              });
            }
            this.getData(JSON.parse(localStorage.getItem(this.localname)))
            this.dataor = 0
          }, 3000); 
      })
    },
    getData(info){
      this.info = info
      if(info.uid == null || info.uid == "null"){//清空缓存第一次进入时找不到info.uid，页面为空页面的处理
        info.uid = 0
      }
      var params = {
          "getdevinfo":[
            {
              "uid": info.uid,
              "type": info.type,
              "addr": info.addr,
              "unittype":"id-name-ready-value-flag-max-min"
            }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
        this.dataor = 1// 接口成功置1
        if(res.data.getdevinfo == undefined){ //数据返回不全失败提示
          this.$message({
            message: '服务器访问失败',
            type: 'error',
            duration: 1000
          });
        }else{
          if(this.clickNumber == 0){ // 延迟判断状态中不接受真实状态
            var st = []
            for(var a = 0; a < 10; a++){
              st.push(
                {
                  id:res.data.getdevinfo[0].unit[a].id, 
                  value:res.data.getdevinfo[0].unit[a].value,
                  name:res.data.getdevinfo[0].unit[a].name,
                  ready:res.data.getdevinfo[0].unit[a].ready
                }
              )
            }
            this.dataShow = st;//展示状态
          }
          this.data = res.data.getdevinfo[0];
          this.alarmstate = res.data.getdevinfo[0].alarmstate
          // 上次上线时间
          this.firstconnecttimeYear = this.formatDate(this.data.firstconnecttime*1000, 'YYYY-MM-DD')
          this.firstconnecttimeHour = this.formatDate(this.data.firstconnecttime*1000, 'H:m')
          // 最近上线时间
          this.connecttime = this.data.connecttime
          this.connecttimeYear = this.formatDate(this.data.connecttime*1000, 'YYYY-MM-DD')
          this.connecttimeHour = this.formatDate(this.data.connecttime*1000, 'H:m')
          //累计运行时间 拿到的分钟转成小时
          this.devtimeshow = localStorage.getItem('累计时长')
          this.totaltime = (this.data.totaltime/60).toFixed(0)
        }
      })
    },
    //控制方法
    switchOpen(id,index){
      var value = 0;
      if(id == 8 || id == 9){
        value = index
        // 延迟保存方法
        clearInterval(this.clickGetData);
        this.clickGetData = null;
        this.clickNumber = 1;  //1秒后走逻辑，所以计时从1开始
        this.dataShow[id].value = value //修改展示状态值
        console.log('下发'+value)
        this.clickGetData = setInterval(() => { 
          this.clickNumber++;
          if(this.data.unit[id].value != this.dataShow[id].value){ // 返回的值和下发值不一致，等待返回
            console.log('错误等待'+this.clickNumber+'秒')
          }else{ // 返回的值和下发值一致，取消等待，拿取真实值
            console.log('正确')
            clearInterval(this.clickGetData);
            this.clickGetData = null;
            this.clickNumber = 0
          }
          if(this.clickNumber > this.$store.state.waittTime){ //等待5秒后，取消等待，拿取真实值 waittTime统一宏变量  计时从1开始，所以等待时间要再+1
            console.log('超时')
            clearInterval(this.clickGetData);
            this.clickGetData = null;
            this.clickNumber = 0
          }
        }, 1000); 
      }else if(id >= 0 && id <= 7){
        if(this.dataShow[id].value == 0){  //开启一个，其余全部关闭
          value = 1
          this.clickNumber = 1 //开启延时
          for(var i = 0; i < 8; i++){
            if(i == id){
              this.dataShow[id].value = value
              
            }else{
              this.dataShow[i].value = 0
            }
          }
          setTimeout(()=>{
            this.clickNumber = 0 //取消延时
          },this.$store.state.waittTime*1000)
        }else{
          value = 0
          this.dataShow[id].value = value
        }
      }
      var params = {
          "setdevinfo":[
              {
                  "uid": this.info.uid,
                  "type": this.info.type,
                  "addr": this.info.addr,
                  "unit":[
                    {
                      id: id,
                      value: value
                    }
                  ]
              }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
          if(res.data.result == 'ok'){
              
          }else{
              this.$notify({
                  title: '操作失败',
                  type: 'warning',
                  duration: 1500
              });
          }
      });
    },
  },
  // 清除定时器
    beforeDestroy() {
        clearInterval(this.timeGetData);
        this.timeGetData = null;
        // 下发控制计时器
        clearInterval(this.clickGetData);
        this.clickGetData = null;
    },
};
</script>

<style scoped>
.el-card {
  background: none;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-card__header {
  background: rgba(89, 129, 165, 0.2);
  padding: 16px 20px;
  border-bottom: none;
}
.box-card {
  /* overflow: hidden; */
}
.temp_card{
  height: 850px;
}
.clearfix{
  font-size: 18px;
}
.shebeiname{
  font-weight: bold;
}
.xtg402_data{
  display: flex;
}
.irf_allk{
  width: 1000px;
  height: 540px;
  margin-top: 90px;
  /* background: #0000002b; */
  border: 1px solid #5981a566;
  box-shadow: 0px 0px 12px #b4e9ff73;
  border-radius: 10px;
}
.irf_nr{
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  align-content: flex-start;
  /* border-bottom: 1px dashed #b8b8b845; */
  padding-bottom: 30px;
}
.irf_nr_flex{
  width: 25%;
  text-align: center;
  margin-bottom: 10px;
  margin-top: 10px;
  display: flex;
  justify-content: space-around;
}
.irf_nr_flex_k{
  /* display: flex; */
  cursor: pointer;
  width: 300px;
}
.irf_but{
    width: 160px;
    height: 50px;
    border: 1px solid #00fcff63;
    box-shadow: 0px 0px 4px 0px #969dc3;
    background: #b0c3da1f;
    text-align: center;
    line-height: 50px;
    font-size: 18px;
    border-radius: 30px; 
    cursor: pointer;
    margin: 0 auto;
    margin-top: 15px;
    overflow: hidden;
}
.irf_but:hover{
    border: 1px solid #00fcff;
    box-shadow: 0px 0px 6px 0px #969dc3;
}
.anjian{
  color: #00fcffa6;
  font-size: 40px;
  border-radius: 50%;
  box-shadow: 0px 0px 4px 0px #969dc3;
  opacity: 0.7;
  filter: grayscale(100%);
    filter: gray;
}
.anjian_active{
  font-size: 40px;
  border-radius: 50%;
  color: #00fcff;
  box-shadow: 0px 0px 7px 0px #5af4ff;
}

/* 前四个按键样式 */
/* 制冷制热 */
.control_xg_img1{
    width: 60px;
    height: 60px;
    margin-right: 40px;
    /* background: #00fcff; */
    background-image: url(../../assets/icon_snow.png);
    background-size: 100% 100%;
}
.control_xg_img2{
    width: 60px;
    height: 60px;
    margin-right: 40px;
    /* background: #00fcff; */
    background-image: url(../../assets/sun.png);
    background-size: 100% 100%;
}
.control_xg_img3{
    width: 60px;
    height: 60px;
    margin-right: 40px;
    /* background: #00fcff; */
    background-image: url(../../assets/start.png);
    background-size: 100% 100%;
}
.control_xg_img4{
    width: 60px;
    height: 60px;
    margin-right: 40px;
    /* background: #00fcff; */
    background-image: url(../../assets/close_.png);
    background-size: 100% 100%;
}
.xg_air_active{
    filter: grayscale(100%);
    filter: gray;
    opacity: 0.7;
}
.xg_air_active_open{
    filter: grayscale(100%);
    filter: gray;
    opacity: 0.4;
}
.irf_nr_top{
  display: flex;
  align-items: center;
  justify-content: space-around;
  margin-bottom: 50px;
}
.irf_nr_top_margin{
  margin-top: 30px;
  margin-bottom: 30px;
}
.irf_top_l,.irf_top_r{
  /* flex: 1; */
  width: 300px;
  display: flex;
  align-items: center;
  /* flex-direction: column; */
}
/* 底部8个按钮的样式 */
.irf_nr_bot_flex{
  width: 12.5%;
  text-align: center;
  margin-bottom: 10px;
  margin-top: 10px;
  display: flex;
  justify-content: space-around;
}
.irf_nr_bot_flex_k{
  /* display: flex; */
  cursor: pointer;
  width: 100%;
}
.irf_bot_but{
    width: 100%;
    height: 50px;
    /* border-bottom: 1px solid #00fcff63; */
    /* box-shadow: 0px 0px 4px 0px #969dc3; */
    /* background: #b0c3da1f; */
    text-align: center;
    line-height: 50px;
    font-size: 18px;
    /* border-radius: 30px;  */
    cursor: pointer;
    margin: 0 auto;
    overflow: hidden;
}
.irf_bot_botbk{
  width: 70px;
  height: 1px;
  background: #00fcff63;
  margin: 0 auto;
}
.irf_nr_bot_flex:hover .irf_bot_botbk{
  background: #00fcff;
}
/* .irf_bot_but:hover{
    border-bottom: 1px solid #00fcff;
} */
.anjian_bot{
  color: #00fcffa6;
  font-size: 25px;
  border-radius: 50%;
  box-shadow: 0px 0px 4px 0px #969dc3;
  opacity: 0.7;
  filter: grayscale(100%);
    filter: gray;
}
.anjian_bot_active{
  font-size: 25px;
  border-radius: 50%;
  color: #00fcff;
  box-shadow: 0px 0px 7px 0px #5af4ff;
}
/* 在线时间 */
.temp_time_mode{
  width: 400px;
  height: 540px;
  /* margin: 0 auto; */
  padding-top: 30px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-top: 90px;
  margin-left: 50px;
}
.temp_peak_name{
  width: 100%;
  font-size: 22px;
  margin-left: 20px;
}
.temp_time{
  margin-top: 80px;
  margin-left: 25px;
  height: 310px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}
.temp_time_line{
  margin-bottom: 90px;
  position: relative;
  top: 11px;
  display: flex;
  font-size: 20px;
}
.temp_time{
  margin-right: 30px;
  color: #d4d3d3;
}
.temp_time .temp_warning_title{
  width: 110px;
}
.temp_time_year{
  width: 150px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_hour{
  width: 40px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_h{
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
  position: relative;
  top: -5px;
}
.temp_time_s{
  color: #5eceff;
  margin-left: 5px;
}
</style>